from selenium import webdriver
from selenium.webdriver.common.by import By
import time


class Mybro:
    def __init__(self,url):
        self.bro=webdriver.Chrome()
        self.bro.get(url)


    # def __del__(self):
    #     self.bro.close()

    #def __del__(self):
    def close(self):
        #self.mainWindow = self.bro.current_window_handle
        #self.bro.switch_to.window(self.mainWindow)
        self.bro.quit()
        #OSError: [WinError 6] 句柄无效
        #self.bro.close()

    def printurl(self):
        start_time=time.time()
        time.sleep(2)
        end_time=time.time()
        print(end_time-start_time,'s')
        print('url')
        print(self.bro.capabilities['version'])

##headline-tabs > ul > li > a
    def link1(self):
        self.biaoti=self.bro.find_elements(By.XPATH,'//*[@id="pane-news"]/div/ul/li[1]/strong/a')
        for item in self.biaoti:
            print(item.text)

def main():
    bro1=Mybro('http://news.baidu.com/')
    bro1.printurl()
    bro1.link1()
    bro1.close()


if __name__ == '__main__':
    main()


